<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hm_Index_module extends CI_Module
{
    public $data;
    public $sid ;
    public function __construct(){
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('admin.Adminuser_model');
        $this->load->model('weixin.weixin_model');
        $this->load->model('member.user_model');
        $this->load->model('order.Orderhotel_model');
        $this->load->model('order.Orderlog_model');
        $this->load->model('order.Orderpaylog_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Orderroom_model');
        $this->load->model('hotel.Hotel_model');
        $this->load->model('order.Ordertourist_model');
        $this->load->model('order.Orderhotelgoods_model');
        $this->load->model('order.Ordernumberlog_model');

        $admin_user_id = $this->session->userdata('admin_user_id');
        if (empty($admin_user_id))  redirect('denglu?type=1');


        $this->config->load('base');
        $this->order_status_array = $this->config->item('order_status');
        $this->pay_status_array = $this->config->item('pay_status');
    }

    public function clear()
    {

        $this->session->sess_destroy();
        redirect('mobile/index');

    }


    /**
     * 首页
     */
    public function index()
    {



        $order_info_table = $this->db->dbprefix("order_info");
        $order_hotel_table = $this->db->dbprefix("order_hotel");

        $where = "" . $order_info_table . ".order_type=1 AND " . $order_info_table . ".sid=$this->sid "; //酒店订单

        //待审核
        $ncheck_sql =  $where." and ".$order_info_table . ".order_status = 1 ";


        $nincom_sql =  $where." and ( $order_hotel_table.start_date <= '".date('Y-m-d')."' or $order_hotel_table.end_date <= '".date('Y-m-d')."'  ) and (".$order_info_table . ".order_status = 4 or ".$order_info_table . ".order_status = 2) ";

        //已入住
        $hincom_sql =  $where." and '".date('Y-m-d')."' >= $order_hotel_table.start_date and '".date('Y-m-d')."' <= $order_hotel_table.end_date and ".$order_info_table . ".order_status = 5 ";

        //已完成
        $finish_sql =  $where." and $order_hotel_table.end_date = '".date('Y-m-d')."' and ".$order_info_table . ".order_status = 6 ";



        $wait_list = $this->Orderhotel_model->get_order_list(200, 1, $ncheck_sql, '*');
        $not_in_list = $this->Orderhotel_model->get_order_list(200, 1, $nincom_sql, '*');
        $in_list = $this->Orderhotel_model->get_order_list(200, 1, $hincom_sql, '*');
        $comp_list = $this->Orderhotel_model->get_order_list(200, 1, $finish_sql, '*');




        $this->data['wait_count'] = count($wait_list) ;
        $this->data['wait_url'] = site_url('hm/index/static_list') ;
        $this->data['not_in_count'] = count($not_in_list) ;
        $this->data['not_in_url'] = site_url('hm/index/static_list?type=2') ;
        $this->data['in_count'] = count($in_list) ;
        $this->data['in_url'] = site_url('hm/index/static_list?type=3') ;
        $this->data['comp_count'] = count($comp_list) ;
        $this->data['comp_url'] = site_url('hm/index/static_list?type=4') ;



        $this->data['common'] = array(
            array(
                'title' => '微农家管理平台',
                'nav'=>'home'
            )
        );

        $this->load->view('default/index', $this->data);


    }


    public function static_list()
    {
        $type = $this->input->get_post('type')?$this->input->get_post('type'):1;

        $order_info_table = $this->db->dbprefix("order_info");
        $order_hotel_table = $this->db->dbprefix("order_hotel");
        $where = "" . $order_info_table . ".order_type=1 AND " . $order_info_table . ".sid=$this->sid "; //酒店订单
        switch ($type)
        {

            case 1:

                //待审核
                $sql =  $where." and ".$order_info_table . ".order_status = 1 ";
                $title = '待审核订单列表';
                break;

            case 2:

                //未入住
                $sql =  $where." and ( $order_hotel_table.start_date <= '".date('Y-m-d')."' or $order_hotel_table.end_date <= '".date('Y-m-d')."'  ) and (".$order_info_table . ".order_status = 4 or ".$order_info_table . ".order_status = 2) ";
                $title = '未入住订单列表';
                break;

            case 3:

                //已入住
                $sql =  $where." and '".date('Y-m-d')."' >= $order_hotel_table.start_date and '".date('Y-m-d')."' <= $order_hotel_table.end_date and ".$order_info_table . ".order_status = 5 ";
                $title = '已入住订单列表';

                break;

            case 4:

                //已完成
                $sql =  $where." and $order_hotel_table.end_date = '".date('Y-m-d')."' and ".$order_info_table . ".order_status = 6 ";
                $title = '已完成订单列表';
                break;
        }

        $order_info_fields = format_table_field(array('order_id', 'order_account','earnest_account', 'order_sn', 'order_status', 'pay_status','cat_type', 'add_time,paid_account,earnest_paid_account'), $this->db->dbprefix("order_info"));
        $contact_fields = format_table_field(array('name as contact_name', 'phone_num', 'wx_nick'), $this->db->dbprefix("order_contacts"));

        $order_hotel_fields = format_table_field(array('number', 'start_date', 'end_date'), $this->db->dbprefix("order_hotel"));

        $fields = $contact_fields . ',' . $order_hotel_fields . ',' . $order_info_fields;

        $result = $this->Orderhotel_model->get_order_list(200, 1, $sql, $fields);

        $this->data['list'] = $this->formate_result_list($result);
        $this->data['common'] = array(
            array(
                'title' => $title,
                'nav'=>'order'
            )
        );

        $this->load->view('default/static_list', $this->data);
    }

    public function formate_result_list($result)
    {


        if($result)
        {
            foreach ($result as $key => $item)
            {
                $order_room_table = $this->db->dbprefix("order_room");
                $hotel_room_table = $this->db->dbprefix("hotel_room");

                $rstr = array();
                $rdate = array();
                $room = $this->Orderroom_model->get_room_info_array("$order_room_table.order_id = {$item['order_id']}", "$hotel_room_table .name,$order_room_table .date ", false, false);

                if ($room)
                {
                    foreach ($room as $index => $value)
                    {
                        $rstr[$index] = $value['name'];
                        $rdate[$index] = fdate($value['date']);
                    }
                }
                $rstr = array_unique($rstr);
                $rdate = array_unique($rdate);

                $result[$key]['rstr'] = !empty($rstr) ? implode(',', $rstr) : '未安排房间';
                $result[$key]['rdate'] = !empty($rdate) ? implode(',', $rdate) : '';


                $result[$key]['edit_url'] = site_url('hm/order/edit/' . $item['order_id']);


                $result[$key]['order_status_name'] = $this->order_status_array[$item['order_status']];
                $result[$key]['pay_status_name'] = $this->pay_status_array[$item['pay_status']];
                $result[$key]['cat_type_name'] = ($item['cat_type']==1)? '门市订单':'游客订单' ;
                $result[$key]['start_date'] = fdate($item['start_date']) ;
                $result[$key]['end_date'] = fdate($item['end_date']) ;
                $result[$key]['order_account'] =  price_format($item['order_account']) ;

            }
        }
        return $result;
    }


    /**
     * 公共头
     *
     * @param array $data
     */
    public function header($data = array()){


        $this->data = array_merge($this->data, $data);
        $this->load->view('default/header', $this->data);
    }

    /**
     * 公共底部
     *
     * @param array $data
     */
    public function footer($data = array()){

        $this->data = array_merge($this->data, $data);
        $this->load->view('default/footer', $this->data);
    }

}